<template>
  <view class="order-page">
    <view class="order-form-container">
      <view class="order-form">
        <!-- 产品编号 -->
        <view class="form-item">
          <view>产品编号：</view>
          <view>{{ product.id || '无' }}</view>
        </view>

        <!-- 产品名称 -->
        <view class="form-item">
          <view>产品名称：</view>
          <view>{{ product.name || '无' }}</view>
        </view>

        <!-- 产品条码 -->
        <view class="form-item">
          <view>产品条码：</view>
          <view>{{ product.barCode || '无' }}</view>
        </view>

        <!-- 产品分类 -->
        <view class="form-item">
          <view>产品分类：</view>
          <view>{{ product.categoryName || '无' }}</view>
        </view>

        <!-- 单位 -->
        <view class="form-item">
          <view>单位：</view>
          <view>{{ product.unitName || '无' }}</view>
        </view>

        <!-- 产品状态 -->
        <view class="form-item">
          <view>产品状态：</view>
          <view>{{ product.status !== undefined ? product.status : '无' }}</view>
        </view>

        <!-- 产品规格 -->
        <view class="form-item">
          <view>产品规格：</view>
          <view>{{ product.standard || '无' }}</view>
        </view>

        <!-- 产品备注 -->
        <view class="form-item">
          <view>产品备注：</view>
          <view>{{ product.remark || '无' }}</view>
        </view>

        <!-- 保质期天数 -->
        <view class="form-item">
          <view>保质期天数：</view>
          <view>{{ product.expiryDay !== undefined ? product.expiryDay : '无' }}</view>
        </view>

        <!-- 基础重量（kg） -->
        <view class="form-item">
          <view>基础重量（kg）：</view>
          <view>{{ product.weight !== undefined ? product.weight : '无' }}</view>
        </view>

        <!-- 采购价格，单位：元 -->
        <view class="form-item">
          <view>采购价格（元）：</view>
          <view>{{ product.purchasePrice !== undefined ? product.purchasePrice : '无' }}</view>
        </view>

        <!-- 销售价格，单位：元 -->
        <view class="form-item">
          <view>销售价格（元）：</view>
          <view>{{ product.salePrice !== undefined ? product.salePrice : '无' }}</view>
        </view>

        <!-- 最低价格，单位：元 -->
        <view class="form-item">
          <view>最低价格（元）：</view>
          <view>{{ product.minPrice !== undefined ? product.minPrice : '无' }}</view>
        </view>

        <!-- 创建时间 -->
        <view class="form-item">
          <view>创建时间：</view>
          <view>{{ formatDate(product.createTime) }}</view>
        </view>
      </view>
	  <!-- 提交按钮 -->
	  <view class="form-item">
	    <button type="button" @click="submitForm">修改</button>
	  </view>
    </view> 
	
  </view>
</template>

<script>
import { getProduct } from "@/sheep/api/erp/product.js";

export default {
  data() {
    return {
      productId: "", // 从路由参数获取产品ID
      product: {}, // 用于存储产品详情
    };
  },

  methods: {
    fetchProductDetails() {
      console.log('获取到的产品ID:=====', this.productId);
      getProduct(this.productId)
        .then((res) => {
          const product = res.data;
          this.product = product;
        })
        .catch(() => {
          uni.showToast({
            title: "获取产品详情失败",
            icon: "none"
          });
        });
    },
    
    // 辅助函数：格式化日期
    formatDate(date) {
      if (!date) return '无';
      const d = new Date(date);
      const year = d.getFullYear();
      const month = String(d.getMonth() + 1).padStart(2, '0');
      const day = String(d.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
	
	submitForm() {
	 // this.productId = uni.getStorageSync('productId');
	  uni.setStorageSync('productId', this.productId);
	  uni.navigateTo({
	    url: "/pages/erp/product/edit"
	  });
	},
  },

  onLoad() {
	   console.log('onLoad:');
    this.productId = uni.getStorageSync('productId');
    console.log('获取到的产品ID:', this.productId);
    this.fetchProductDetails();
  }, 
  onShow() {
	   console.log('onShow:');
    this.fetchProductDetails();
  }
};
</script>
 